package com.qinge.blog.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

/**
 * @author QDW
 * @date 2022/05/10 04:39
 * @description MyBatis Generator 自动创建,对应数据表为：blog_user
 */
public class BlogUser implements Serializable, UserDetails {
    /**
     */
    private String id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 账号
     */
    private String nickname;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 上一次登录时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastLoginTime;

    /**
     * 账号是否可用。默认为1（可用）
     */
    @TableField(fill = FieldFill.INSERT)
    private Boolean enabled;

    /**
     * 是否过期。默认为1（没有过期）
     */
    @TableField(fill = FieldFill.INSERT)
    private Boolean accountNonExpired;

    /**
     * 账号是否锁定。默认为1（没有锁定）
     */
    @TableField(fill = FieldFill.INSERT)
    private Boolean accountNonLocked;

    /**
     * 证书（密码）是否过期。默认为1（没有过期）
     */
    @TableField(fill = FieldFill.INSERT)
    private Boolean credentialsNonExpired;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 电子邮箱
     */
    private String email;

    /**
     * 登录省份
     */
    private String loginProvince;

    /**
     * 登录城市
     */
    private String loginCity;

    /**
     * 经度
     */
    private String loginLongitude;

    /**
     * 维度
     */
    private String loginLatitude;

    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 权限
     */
    @JsonIgnore
    @TableField(exist=false)
    private Set<GrantedAuthority> authorities;

    /**
     * 角色
     */
    @TableField(exist = false)
    private List<String> roles;

    private static final long serialVersionUID = 1L;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname == null ? null : nickname.trim();
    }
    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }


    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar == null ? null : avatar.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getLoginProvince() {
        return loginProvince;
    }

    public void setLoginProvince(String loginProvince) {
        this.loginProvince = loginProvince == null ? null : loginProvince.trim();
    }

    public String getLoginCity() {
        return loginCity;
    }

    public void setLoginCity(String loginCity) {
        this.loginCity = loginCity == null ? null : loginCity.trim();
    }

    public String getLoginLongitude() {
        return loginLongitude;
    }

    public void setLoginLongitude(String loginLongitude) {
        this.loginLongitude = loginLongitude == null ? null : loginLongitude.trim();
    }

    public String getLoginLatitude() {
        return loginLatitude;
    }

    public void setLoginLatitude(String loginLatitude) {
        this.loginLatitude = loginLatitude == null ? null : loginLatitude.trim();
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * toString
     * @return
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", username=").append(username);
        sb.append(", nickname=").append(nickname);
        sb.append(", password=").append(password);
        sb.append(", lastLoginTime=").append(lastLoginTime);
        sb.append(", enabled=").append(enabled);
        sb.append(", accountNonExpired=").append(accountNonExpired);
        sb.append(", accountNonLocked=").append(accountNonLocked);
        sb.append(", credentialsNonExpired=").append(credentialsNonExpired);
        sb.append(", avatar=").append(avatar);
        sb.append(", email=").append(email);
        sb.append(", loginProvince=").append(loginProvince);
        sb.append(", loginCity=").append(loginCity);
        sb.append(", loginLongitude=").append(loginLongitude);
        sb.append(", loginLatitude=").append(loginLatitude);
        sb.append(", updateTime=").append(updateTime);
        sb.append(", createTime=").append(createTime);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }


    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
        Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
        SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet(new BlogUser.AuthorityComparator());
        Iterator var2 = authorities.iterator();

        while(var2.hasNext()) {
            GrantedAuthority grantedAuthority = (GrantedAuthority)var2.next();
            Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
            sortedAuthorities.add(grantedAuthority);
        }

        return sortedAuthorities;
    }

    private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {
        private static final long serialVersionUID = 560L;

        private AuthorityComparator() {
        }

        @Override
        public int compare(GrantedAuthority g1, GrantedAuthority g2) {
            if (g2.getAuthority() == null) {
                return -1;
            } else {
                return g1.getAuthority() == null ? 1 : g1.getAuthority().compareTo(g2.getAuthority());
            }
        }
    }


    public enum Column {
        id("id", "id", "VARCHAR", false),
        username("username", "username", "VARCHAR", false),
        nickname("nickname", "nickname", "VARCHAR", false),
        password("password", "password", "VARCHAR", false),
        lastLoginTime("last_login_time", "lastLoginTime", "TIMESTAMP", false),
        enabled("enabled", "enabled", "BIT", false),
        accountNonExpired("account_non_expired", "accountNonExpired", "BIT", false),
        accountNonLocked("account_non_locked", "accountNonLocked", "BIT", false),
        credentialsNonExpired("credentials_non_expired", "credentialsNonExpired", "BIT", false),
        avatar("avatar", "avatar", "VARCHAR", false),
        email("email", "email", "VARCHAR", false),
        loginProvince("login_province", "loginProvince", "VARCHAR", false),
        loginCity("login_city", "loginCity", "VARCHAR", false),
        loginLongitude("login_longitude", "loginLongitude", "VARCHAR", false),
        loginLatitude("login_latitude", "loginLatitude", "VARCHAR", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        createTime("create_time", "createTime", "TIMESTAMP", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        /**
         * value
         * @return
         */
        public String value() {
            return this.column;
        }

        /**
         * getValue
         * @return
         */
        public String getValue() {
            return this.column;
        }

        /**
         * getJavaProperty
         * @return
         */
        public String getJavaProperty() {
            return this.javaProperty;
        }

        /**
         * getJdbcType
         * @return
         */
        public String getJdbcType() {
            return this.jdbcType;
        }

        /**
         * Column
         * @param column
         * @param javaProperty
         * @param jdbcType
         * @param isColumnNameDelimited
         * @return
         */
        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        /**
         * desc
         * @return
         */
        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        /**
         * asc
         * @return
         */
        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        /**
         * excludes
         * @param excludes
         * @return
         */
        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        /**
         * all
         * @return
         */
        public static Column[] all() {
            return Column.values();
        }

        /**
         * getEscapedColumnName
         * @return
         */
        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        /**
         * getAliasedEscapedColumnName
         * @return
         */
        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
